package cn.hs.ph.servlet;

import cn.hs.ph.dao.ShopDao;
import cn.hs.ph.dao.impl.ShopDaoImpl;
import cn.hs.ph.entity.Shop;
import cn.hs.ph.util.Result;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/shop")
public class ShopServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        ShopDao shopDao=new ShopDaoImpl();
        if ("selectAll".equals(action)){
            String keyword = req.getParameter("keyword");
            String shopPhone = req.getParameter("phone");
            String startTime = req.getParameter("startTime");
            String endTime = req.getParameter("endTime");
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            //封装到map集合中
            Map<String,String> map=new HashMap<>();
            map.put("shopName",keyword);
            map.put("shopPhone",shopPhone);
            map.put("startTime",startTime);
            map.put("endTime",endTime);
            map.put("pageNum",pageNum);
            map.put("pageSize",pageSize);
            List<Shop> list = shopDao.selectAll(map);
            int total = shopDao.selectCount(map);
            //给出响应
            Map resultMap=new HashMap();
            resultMap.put("list",list);
            resultMap.put("total",total);
            Result success = Result.success(resultMap);
            resp.getWriter().write(JSON.toJSONString(success));
        }else if ("add".equals(action)){
            //获取数据封装shop对象
            String shopAddress = req.getParameter("shopAddress");
            String shopCity = req.getParameter("shopCity");
            String shopDescribe = req.getParameter("shopDescribe");
            String shopDistrict = req.getParameter("shopDistrict");
            String shopImage = req.getParameter("shopImage");
            String shopLatitude = req.getParameter("shopLatitude");
            String shopLongitude = req.getParameter("shopLongitude");
            String shopName = req.getParameter("shopName");
            String shopPhone = req.getParameter("shopPhone");
            String shopProvince = req.getParameter("shopProvince");
            Shop shop=new Shop( shopAddress,shopCity,  shopDescribe,shopDistrict,shopImage,shopLatitude,  shopLongitude,shopName,shopPhone,shopProvince);
            //连接数据库
            int num = shopDao.insert(shop);
            Result result = null;
            if (num != 0) {
                result = Result.success("添加成功");
            } else {
                result = Result.error("添加失败");
            }
            resp.getWriter().write(JSON.toJSONString(result));
        }else if ("modify".equals(action)){
            //获取数据封装shop对象
            String shopAddress = req.getParameter("shopAddress");
            String shopCity = req.getParameter("shopCity");
            String shopDescribe = req.getParameter("shopDescribe");
            String shopDistrict = req.getParameter("shopDistrict");
            String shopImage = req.getParameter("shopImage");
            String shopLatitude = req.getParameter("shopLatitude");
            String shopLongitude = req.getParameter("shopLongitude");
            String shopName = req.getParameter("shopName");
            String shopPhone = req.getParameter("shopPhone");
            String shopProvince = req.getParameter("shopProvince");
            String id = req.getParameter("shopId");
            int shopId=0;
            if (id != null && !"".equals(id)) {
                shopId = Integer.parseInt(id);
            }
            Shop shop=new Shop(shopId,shopAddress,shopCity,shopDescribe,shopDistrict,shopImage,shopLatitude,  shopLongitude,shopName,shopPhone,shopProvince);
            int num = shopDao.update(shop);
            Result result = null;
            if (num != 0) {
                result = Result.success("修改成功");
            } else {
                result = Result.error("修改失败");
            }
            resp.getWriter().write(JSON.toJSONString(result));
        }else if ("delete".equals(action)){
            String Id = req.getParameter("shopId");
            int shopId = 0;
            if (Id != null && !"".equals(Id)) {
                shopId = Integer.parseInt(Id);
            }
            int num = shopDao.delete(shopId);
            Result result = null;
            if (num != 0) {
                result = Result.success("删除成功");
            } else {
                result = Result.error("删除失败");
            }
            resp.getWriter().write(JSON.toJSONString(result));
        }else if (action.equals("delete")){
            int shopId = Integer.parseInt(req.getParameter("shopId"));
            int num = shopDao.delete(shopId);
            Result result=null;
            if (num!=0){
                result=Result.success("删除成功");
            }else {
                result=Result.error("删除失败");
            }
            resp.getWriter().write(JSON.toJSONString(result));
        }else if (action.equals("modify")){

        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
